/* ===========================================================================
 *
 *       Filename:  xyb_handwriting_demo.cpp
 *
 *    Description:  handwritor debug (qt)
 *
 * - Mode: 644
 * - NOTE. NOT use "dos"
 * - Updated: TODO
 *
 *        Version:  1.0.0
 *        Created:  2015-04-16 10:13:08
 *       Revision:  1.0.0
 *       Compiler:  moc/g++
 *
 *         Author:  Yui Wong, email: yuiwong@126.com
 *   Organization:  ""
 *        License:  LGPLv3
 *
 *  This file is part of portable-this->inputmethod.yw.
 *
 *  portable-this->inputmethod.yw
 *  is free software: you can redistribute it
 *  and/or modify it under the terms of the GNU General Public License as
 *  published by the Free Software Foundation, either version 3 of the
 *  License, or (at your option) any later version.
 *
 *  portable-this->inputmethod.yw
 *  is distributed in the hope that it will be
 *  useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with portable-this->inputmethod.yw.
 *  If not, see <http://www.gnu.org/licenses/>.
 *
 * ===========================================================================
 */


#include "xyb_handwriting_demo.h"

#include <QtCore/QLocale>
#include <QtCore/QDebug>
#if (ID_QT_VERSION < 0x050000)
#	include <QtGui/QApplication>
#else
#	include <QtWidgets/QApplication>
#endif

#include "windows.h"


/*
 * ===========================================================================
 * constructor and ~
 * ===========================================================================
 */
xyb_handwriting_demo::xyb_handwriting_demo (const char * conf,
	QWidget * parent)
	: QMainWindow(parent) {
#	if defined(PIMETHOD_DEBUG)
	qDebug() << __FILE__ << __LINE__ << __func__;
#	endif /* if defined(PIMETHOD_DEBUG) */

	if (conf) {
		this->conf_dump = strdup(conf);
	} else {
		this->conf_dump = NULL;
	}

	this->ui_setup();
	this->signal_connect();
} /* xyb_handwriting_demo::xyb_handwriting_demo */


xyb_handwriting_demo::~xyb_handwriting_demo (void) {
#	if defined(PIMETHOD_DEBUG)
	qDebug() << __FILE__ << __LINE__ << __func__;
#	endif /* if defined(PIMETHOD_DEBUG) */

	this->signal_disconnect();

	if (this->conf_dump) {
		free(this->conf_dump);
		this->conf_dump = NULL;
	}

	/*
	 * XXX-NOTE: in normal case: ui elemnets:
	 * 母控件析构时 会自动释放其子控件的内存
	 * => all in layout => delete all layout and central_widget is ok
	 */
	delete this->title;
	delete this->input_label;
	delete this->hwdbg;
	delete this->prompt;
	delete this->btn_clear;
	delete this->btn_cancel;
	delete this->_master_layout;
	delete this->central_widget;

	if (this->input_manager) {
		this->input_manager->method_release(false);/* bonded => false */
		delete this->input_manager;
		this->input_manager = NULL;
	}

	qDebug() << "DONE";
} /* xyb_handwriting_demo::~xyb_handwriting_demo */


/*
 * ===========================================================================
 * end of constructor and ~
 * ===========================================================================
 */


/*
 * ===========================================================================
 * ui
 * ===========================================================================
 */
#define __STLST_TL \
	"color: #ff0000"

#define __STLST_LBL \
	"background-color: #ffffff; color: #000000; border-radius: 8px"

#define __STLST_UN \
	"background-color: #ffffff; color: #000000; font-weight: bold; " \
	"border-radius: 8px"
#define __STLST_PPT \
	"color: #ff0000; font-weight: bold; border-radius: 8px"
#define __STLST_BTN \
	"background-color: #b22222; color: #000000; border-radius: 8px"


int xyb_handwriting_demo::ui_setup (void) {
	QPalette palette;
	QFont font;
	QSpacerItem * spacer;

#	if defined(PIMETHOD_DEBUG)
	qDebug() << __FILE__ << __LINE__ << __func__;
#	endif /* if defined(PIMETHOD_DEBUG) */

	/* obj name */
	if (this->objectName().isEmpty()) {
		this->setObjectName(QString::fromUtf8("xyb_handwriting_demo"));
	}

	/* size */
	this->resize(800, 600);
	/* bg */
	this->setAutoFillBackground(true);
	/* rgba */
	palette.setColor(QPalette::Background, QColor(0xff, 0xff, 0xff, 0x80));
	this->setPalette(palette);
	/* locale */
	this->setLocale(QLocale(QLocale::Chinese, QLocale::China));

	/* new central widget and to this */
	this->central_widget = new QWidget(this);
	this->central_widget->setEnabled(true);
	this->central_widget->setLocale(QLocale(QLocale::Chinese,
		QLocale::China));


	this->_master_layout = new QVBoxLayout(this->central_widget);
	this->_master_layout->setContentsMargins(0, 0, 0, 0);/* l, t, r, b */
	this->title = new QLabel(this->central_widget);
	this->title->setAlignment(Qt::AlignCenter);
	font.setPixelSize(36);
	this->title->setFont(font);
	this->title->setStyleSheet(__STLST_TL);

	this->master_layout = new QHBoxLayout();
	this->master_layout->setContentsMargins(0, 0, 0, 0);


	this->center_layout = new QVBoxLayout();
	this->center_layout->setContentsMargins(0, 0, 0, 20);

	this->input_layout = new QHBoxLayout();
	this->input_label = new QLabel(this->central_widget);
	this->input_label->setMinimumHeight(48);
	this->input_label->setAlignment(Qt::AlignCenter);
	font.setPixelSize(24);
	this->input_label->setFont(font);
	this->input_label->setStyleSheet(__STLST_LBL);

	this->hwdbg = new QLineEdit(this->central_widget);
	this->hwdbg->setMinimumHeight(48);
	this->hwdbg->setAlignment(Qt::AlignCenter);
	this->hwdbg->setFont(font);
	this->hwdbg->setStyleSheet(__STLST_UN);

	this->input_layout->addWidget(this->input_label);
	this->input_layout->addWidget(this->hwdbg);

	this->input_layout->setStretch(0, 1);
	this->input_layout->setStretch(1, 2);

	this->prompt = new QLabel(this->central_widget);
	this->prompt->setStyleSheet(__STLST_PPT);
	this->prompt->setMinimumHeight(36);
	/* this->prompt->setMaximumHeight(48); */
	this->prompt->setAlignment(Qt::AlignCenter);
	this->prompt->setFont(font);

	this->input_manager = new xyb_handwriting_manager();
	this->input = this->input_manager->method(this->hwdbg,
		this->conf_dump, NULL);

	spacer = new QSpacerItem(36, 24);
	this->center_layout->addItem(spacer);
	this->center_layout->addLayout(this->input_layout);
	this->center_layout->addWidget(this->prompt);
	this->center_layout->addWidget((QWidget *)this->input);
	spacer = new QSpacerItem(36, 24);
	this->center_layout->addItem(spacer);

	this->center_layout->setAlignment(this->input_layout, Qt::AlignCenter);
	this->center_layout->setAlignment(this->prompt, Qt::AlignCenter);
	this->center_layout->setAlignment(this->input, Qt::AlignCenter);

	this->center_layout->setStretch(0, 2);
	this->center_layout->setStretch(1, 1);
	this->center_layout->setStretch(2, 1);
	this->center_layout->setStretch(3, 3);
	this->center_layout->setStretch(4, 1);


	this->right_layout = new QVBoxLayout();
	this->right_layout->setContentsMargins(0, 0, 5, 0);

	this->btn_clear = new QPushButton(this->central_widget);
	this->btn_clear->setFixedSize(164, 64);
	this->btn_clear->setFont(font);
	this->btn_clear->setStyleSheet(__STLST_BTN);

	this->btn_cancel = new QPushButton(this->central_widget);
	this->btn_cancel->setFixedSize(164, 64);
	this->btn_cancel->setStyleSheet(__STLST_BTN);
	this->btn_cancel->setFont(font);

	spacer = new QSpacerItem(36, 24);
	this->right_layout->addItem(spacer);
	this->right_layout->addWidget(this->btn_clear);
	spacer = new QSpacerItem(36, 24);
	this->right_layout->addItem(spacer);
	this->right_layout->addWidget(this->btn_cancel);
	spacer = new QSpacerItem(36, 24);
	this->right_layout->addItem(spacer);

	this->right_layout->setAlignment(this->btn_clear, Qt::AlignCenter);
	this->right_layout->setAlignment(this->btn_cancel, Qt::AlignCenter);

	this->right_layout->setStretch(0, 4);
	this->right_layout->setStretch(1, 1);
	this->right_layout->setStretch(2, 1);
	this->right_layout->setStretch(3, 1);
	this->right_layout->setStretch(4, 1);


	spacer = new QSpacerItem(36, 24);
	this->master_layout->addItem(spacer);
	this->master_layout->addLayout(this->center_layout);
	this->master_layout->addLayout(this->right_layout);

	this->master_layout->setStretch(0, 1);
	this->master_layout->setStretch(1, 4);
	this->master_layout->setStretch(2, 1);


	this->_master_layout->addWidget(this->title);
	this->_master_layout->addLayout(this->master_layout);
	this->_master_layout->setAlignment(this->title, Qt::AlignCenter);
	this->_master_layout->setAlignment(this->master_layout, Qt::AlignCenter);

	this->_master_layout->setStretch(0, 1);
	this->_master_layout->setStretch(1, 8);

	this->setCentralWidget(this->central_widget);

	ui_retranslate();

	/* final of ui setup */
	QMetaObject::connectSlotsByName(this);

	return 0;
} /* xyb_handwriting_demo:ui_setup */


void xyb_handwriting_demo::ui_retranslate (void) {
#	if defined(PIMETHOD_DEBUG)
	qDebug() << __FILE__ << __LINE__ << __func__;
#	endif /* if defined(PIMETHOD_DEBUG) */

	this->setWindowTitle(QString::fromUtf8("中文手写测试"));
	this->input_label->setText(QString::fromUtf8("输入"));
	this->btn_clear->setText(QString::fromUtf8("清除"));
	this->btn_cancel->setText(QString::fromUtf8("取消"));
	this->title->setText(QString::fromUtf8("手写输入中文"));
} /* xyb_handwriting_demo_ui::ui_retranslate */
/*
 * ===========================================================================
 * end of ui
 * ===========================================================================
 */


void xyb_handwriting_demo::signal_connect (void) {
#	if defined(PIMETHOD_DEBUG)
	qDebug() << __FILE__ << __LINE__ << __func__;
#	endif /* if defined(PIMETHOD_DEBUG) */

	QObject::connect(this->btn_clear, SIGNAL(clicked()), this,
		SLOT(handle_clear()));
	QObject::connect(this->btn_cancel, SIGNAL(clicked()), this,
		SLOT(handle_cancel()));
	QObject::connect(this->input,
		SIGNAL(char_chose(const QWidget *, const char *)),
		this, SLOT(handle_chose(const QWidget *, const char *)));
} /* xyb_handwriting_demo::signal_connect */


void xyb_handwriting_demo::signal_disconnect (void) {
#	if defined(PIMETHOD_DEBUG)
	qDebug() << __FILE__ << __LINE__ << __func__;
#	endif /* if defined(PIMETHOD_DEBUG) */

	QObject::disconnect(this->btn_clear, SIGNAL(clicked()), this,
		SLOT(handle_clear()));
	QObject::disconnect(this->btn_cancel, SIGNAL(clicked()), this,
		SLOT(handle_cancel()));
	QObject::disconnect(this->input,
		SIGNAL(char_chose(const QWidget *, const char *)),
		this, SLOT(handle_chose(const QWidget *, const char *)));
} /* xyb_handwriting_demo::signal_disconnect */


/*
 * ===========================================================================
 * handlers
 * ===========================================================================
 */
void xyb_handwriting_demo::handle_cancel (void) {
#	if defined(PIMETHOD_DEBUG)
	qDebug() << __FILE__ << __LINE__ << __func__;
#	endif /* if defined(PIMETHOD_DEBUG) */

	this->close();
} /* idh_launch::handle_cancel */


void xyb_handwriting_demo::handle_clear (void) {
#	if defined(PIMETHOD_DEBUG)
	qDebug() << __FILE__ << __LINE__ << __func__;
#	endif /* if defined(PIMETHOD_DEBUG) */

	this->hwdbg->setText(QString(""));
	this->hwdbg->setFocus();
	this->prompt->setText(QString::fromUtf8(""));
} /* idh_launch::handle_clear */


void xyb_handwriting_demo::closeEvent (QCloseEvent * evt) {
#	if defined(PIMETHOD_DEBUG)
	qDebug() << __FILE__ << __LINE__ << __func__;
#	endif /* if defined(PIMETHOD_DEBUG) */
} /* xyb_handwriting_demo::closeEvent */


void xyb_handwriting_demo::handle_chose (const QWidget * focus,
	const char * utf8) {
	QString d = QString::fromUtf8(utf8);
	QString s = this->hwdbg->text();

#	if defined(PIMETHOD_DEBUG)
	qDebug() << __FILE__ << __LINE__ << __func__;
#	endif /* if defined(PIMETHOD_DEBUG) */

	if (focus == this->hwdbg) {
		s.append(d);
		this->hwdbg->setText(s);
	} else {
#		if defined(PIMETHOD_DEBUG)
		qDebug() << __FILE__ << __LINE__ << __func__ << "BAD FOCUS";
#		endif /* if defined(PIMETHOD_DEBUG) */
	}
} /* xyb_handwriting_demo::handle_chose */
/*
 * ===========================================================================
 * end of handlers
 * ===========================================================================
 */
/*
 * xyb_handwriting_demo.cpp
 */
